//
//  RPNoDataView.m
//  ShowYork
//
//  Created by hxh on 2021/3/11.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "RPNoDataView.h"

@interface RPNoDataView ()
@property (nonatomic,strong)UILabel *titleLb;
@property (nonatomic,strong)UIImageView *imgView;
@property (nonatomic,strong)UIButton *button;
@property (nonatomic,assign)BOOL height;
@property (nonatomic,strong)CAGradientLayer *gradientLayer;
@end

@implementation RPNoDataView

- (CAGradientLayer *)gradientLayer{
    if (!_gradientLayer) {
        _gradientLayer =  [CAGradientLayer layer];
        _gradientLayer.frame = self.button.bounds;
        _gradientLayer.colors = self.layerColors;
    }
    return _gradientLayer;
}

- (instancetype)initWithNoDataTitle:(NSString * __nullable)title image:(NSString * __nullable)image area:(CGFloat)area{
    if (self = [super init]) {
        
        _displayArea = area;
        
        _text = title;
       
        _image = image;
        
        _layerColors = @[(id)RGBA(247, 131, 97, 1).CGColor,(id)RGBA(245, 75, 100, 1).CGColor];
        
        [self setUI];
    }
    return self;
}

- (instancetype)initWithNoDataTitle:(NSString *)title image:(NSString *)image{
    return [self initWithNoDataTitle:@"无内容" image:@"mine_noList_tag" area:kScreenH];
}



- (void)setUI{
    
    self.frame = CGRectMake(0, 0, kScreenW, self.displayArea);

    self.titleLb = [[UILabel alloc]init];
    self.titleLb.textColor = RGBA(153, 153, 153, 1);
    self.titleLb.font = font(10);
    self.titleLb.textAlignment = NSTextAlignmentCenter;
    self.titleLb.text = self.text;

    [self addSubview:self.titleLb];
    
    self.imgView = [[UIImageView alloc]init];
    self.imgView.image = [UIImage imageNamed:self.image];
    [self addSubview:self.imgView];
    
    self.button = [[UIButton alloc]init];
    [self.button setTitle:@"确定" forState:UIControlStateNormal];
    [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.button.titleLabel.font = font(14);
    self.button.layer.cornerRadius = 17;
    self.button.layer.masksToBounds = YES;
    self.button.hidden = YES;
    [self.button addTarget:self action:@selector(confirmMethod) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.button];
}

- (void)showInView:(UIView *)mainView{
    [mainView addSubview:self];
}
- (void)showInView:(UIView *)mainView location:(CGPoint)point{
    [mainView addSubview:self];
    
    self.cl_x = point.x;
    self.cl_y = point.y;
}

- (void)setDisplayArea:(CGFloat)displayArea{
    _displayArea = displayArea;
}
- (void)setText:(NSString *)text{
    _text = text;
    self.titleLb.text = text;
}
- (void)setFont:(UIFont *)font{
    _font = font;
    self.titleLb.font = font;
}
-(void)setTextColor:(UIColor *)textColor{
    _textColor = textColor;
    self.titleLb.textColor = textColor;
}
- (void)setImage:(NSString *)image{
    _image = image;
    self.imgView.image = [UIImage imageNamed:image];
}
- (void)setUrl:(NSString *)url{
    _url = url;
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:url]];
}
- (void)setShowBtn:(BOOL)showBtn{
    _showBtn = showBtn;
    self.button.hidden = !showBtn;
}
- (void)setButtonTitleFont:(UIFont *)buttonTitleFont{
    _buttonTitleFont = buttonTitleFont;
    self.button.titleLabel.font = buttonTitleFont;
}
- (void)setButtonTitleColor:(UIColor *)buttonTitleColor{
    _buttonTitleColor = buttonTitleColor;
    [self.button setTitleColor:buttonTitleColor forState:UIControlStateNormal];
}
- (void)setButtonTitle:(NSString *)buttonTitle{
    _buttonTitle = buttonTitle;
    [self.button setTitle:buttonTitle forState:UIControlStateNormal];
}
- (void)setLayerColors:(NSArray *)layerColors{
    _layerColors = layerColors;
    self.gradientLayer.colors = layerColors;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGSize size = self.imgView.image.size;
    
    self.imgView.frame = CGRectMake(0, (self.cl_height - size.height)/2 - 50,size.width,size.height);
    self.imgView.cl_centerX = self.cl_centerX;
    
    self.titleLb.cl_centerX = self.cl_centerX;
    self.titleLb.cl_y = CGRectGetMaxY(self.imgView.frame) + 5;
    self.titleLb.cl_width = kScreenW;
    self.titleLb.cl_height = 30;
    
    if (self.showBtn) {
        self.button.frame = CGRectMake(0, CGRectGetMaxY(self.titleLb.frame) + 48, 90, 34);
        self.button.cl_centerX = self.cl_centerX;
        [self.button.layer insertSublayer:self.gradientLayer atIndex:0];
    }
    
}

- (void)confirmMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(noDataButtonMethod)]) {
        [self.delegate noDataButtonMethod];
    }
}

@end
